const models = require("../db/db");
const express = require("express");
const router = express.Router();
const mysql = require("mysql");
const $sql = require("../db/sqlMap");

const conn = mysql.createConnection(models.mysql);
conn.connect();
// 查询一个的接口
router.post("/findOne", (req, res) => {
  const user = req.body;
  const sel_email =
    $sql.goods.select + " where g.goods_id = '" + user.productID + "'";
  console.log(sel_email);
  conn.query(sel_email, user.productID, (error, results) => {
    if (error) {
      throw error;
    }
    if (results != null) {
      res.send(results);
    }
  });
});

// 搜索查询的接口
router.post("/search", (req, res) => {
  const user = req.body;
  const sel_email =
    $sql.goods.select +
    " where g.goods_name  like concat('%'," +
    user.search +
    ",'%')";
  console.log(sel_email);
  conn.query(sel_email, user.search, (error, results) => {
    if (error) {
      throw error;
    }
    if (results != null) {
      res.send(results);
    }
  });
});

// 查询一个分类的接口
router.post("/findType", (req, res) => {
  const user = req.body;
  const sel_type =
    $sql.goods.select + " where g.type_id = '" + user.type_id + "'";
  const sel = $sql.goods.select;
  if (user.type_id == 0) {
    conn.query(sel, (error, results) => {
      if (error) {
        throw error;
      }
      if (results != null) {
        res.send(results);
      }
    });
  } else {
    conn.query(sel_type, user.type_id, (error, results) => {
      if (error) {
        throw error;
      }
      if (results != null) {
        res.send(results);
      }
    });
  }
});

// 查询全部
router.get("/findAll", (req, res) => {
  const sel_email = $sql.goods.select;
  const user = req.body;
  conn.query(sel_email, user.limit, (error, results) => {
    if (error) {
      throw error;
    }
    console.log(results);
    if (results != null) {
      res.send(results); // -1 表示查询不到，用户不存在，即邮箱填写错误
    }
  });
});

// 查询类别
router.get("/classify", (req, res) => {
  const sel_email = $sql.goods.classify;
  conn.query(sel_email, (error, results) => {
    if (error) {
      throw error;
    }
    if (results != null) {
      res.send(results); // -1 表示查询不到，用户不存在，即邮箱填写错误
    }
  });
});
// 商品分类的接口
router.post("/categorySearch", (req, res) => {
  const sel_type = $sql.goods.categorySearch;
  const type = req.body;
  conn.query(sel_type, type.id, (error, results) => {
    if (error) {
      // console.log(error, "results++++++++++++++++categorySearch");
      throw error;
    }
    console.log(results, "results++++++++++++++++categorySearch");
    if (results != null) {
      res.send(results); // -1 表示查询不到，用户不存在，即邮箱填写错误
    }
  });
  // 商品分类的商品条数接口
  router.post("/categorySearchTotal", (req, res) => {
    const sel_type = $sql.goods.total;
    const type = req.body;
    conn.query(sel_type, type.id, (error, results) => {
      if (error) {
        // console.log(error, "results++++++++++++++++categorySearch");
        throw error;
      }
      console.log(results, "results++++++++++++++++categorySearch");
      if (results != null) {
        res.send(results); // -1 表示查询不到，用户不存在，即邮箱填写错误
      }
    });
  });
});

module.exports = router;
